package com.dingwei.lesyndrome.ui.activity;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dingwei.lesyndrome.R;
import com.dingwei.lesyndrome.adapter.AddressListAdapter;
import com.dingwei.lesyndrome.bean.AddressListBean;
import com.dingwei.lesyndrome.mvp.presenter.AddressListPresenter;
import com.dingwei.lesyndrome.mvp.view.Contract;
import com.dingwei.lesyndrome.utils.DialogUtil;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.utils.DataCleanManager;
import com.lihao.baselibrary.weight.TitleBar;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.entity.LocalMedia;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created time 2018/3/9.
 * Creator 南柯一梦丶 on
 * Personal signature 余生还长 何必慌张
 * Page description :
 */

public class AddressListActivity extends BaseActivity implements Contract.AddressListView {
    AddressListPresenter addressListPresenter = new AddressListPresenter(this);
    @Bind(R.id.title_bar)
    TitleBar titleBar;
    @Bind(R.id.m_recyclerView)
    RecyclerView mRecyclerView;
    @Bind(R.id.img_no_data)
    ImageView imgNoData;
    @Bind(R.id.tv_no_data)
    TextView tvNoData;
    @Bind(R.id.no_data_layout)
    RelativeLayout noDataLayout;
    @Bind(R.id.refresh_layout)
    SmartRefreshLayout refreshLayout;
    @Bind(R.id.no_net_btn)
    TextView noNetBtn;
    @Bind(R.id.no_net_layout)
    RelativeLayout noNetLayout;

    AddressListAdapter adapter;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_address_list;
    }

    @Override
    protected void initData() {
        refreshLayout.autoRefresh();
    }

    @Override
    protected void initView() {
        adapter = new AddressListAdapter(BaseActivity.context);

        LinearLayoutManager layoutManager = new LinearLayoutManager(context);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setNestedScrollingEnabled(false);
        mRecyclerView.setAdapter(adapter);

        refreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                page = page + 1;
                addressListPresenter.initData(page,false,false);
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                addressListPresenter.initData(page,true,false);
            }
        });

        adapter.setOnAddressInterfaceListener(new AddressListAdapter.addressInterFace() {
            @Override
            public void delete(final int position) {

                DialogUtil.showAlertDialogChoose(BaseActivity.context, "温馨提醒", "是否确认删除", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        switch (which) {
                            case DialogUtil.BUTTON1:
                                dialog.dismiss();
                                break;
                            case DialogUtil.BUTTON2:
                                dialog.dismiss();
                                AddressListBean.DataBean bean = adapter.getItem(position);
                                addressListPresenter.delete(bean.id,position);
                                break;
                        }
                    }
                });

            }
        });
        titleBar.setOnMenuListener(new TitleBar.OnMenuListener() {
            @Override
            public void onMenuClick() {
                intent = new Intent(BaseActivity.context,MapActivity.class);
                startActivityForResult(intent,0);
            }
        });

        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                AddressListBean.DataBean bean = adapter.getItem(position);
                intent = new Intent(BaseActivity.context,MapActivity.class);
                intent.putExtra("dataBean",bean);
                startActivityForResult(intent,0);
            }
        });
    }

    @Override
    public void onFail() {
        showData("fail");
    }

    @Override
    public void onRefresh(AddressListBean bean) {
        adapter.clear();
        adapter.addAll(bean.data);
        refreshLayout.finishRefresh();
        showData("success");
    }

    @Override
    public void onLoadMore(AddressListBean bean) {
        adapter.addAll(bean.data);
        refreshLayout.finishLoadmore();
        showData("success");
    }


    public void showData(String state) {
        if (state.equals("success")) {
            noNetLayout.setVisibility(View.GONE);
            refreshLayout.setVisibility(View.VISIBLE);
            if (adapter.getAllData() != null && adapter.getAllData().size() > 0) {
                mRecyclerView.setVisibility(View.VISIBLE);
                noDataLayout.setVisibility(View.GONE);
            } else {
                mRecyclerView.setVisibility(View.GONE);
                noDataLayout.setVisibility(View.VISIBLE);
            }
        } else {
            refreshLayout.setVisibility(View.GONE);
            noNetLayout.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void deleteSuccess(int position) {
        adapter.remove(position);
        showData("success");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != Activity.RESULT_OK) {
            return;
        }
        switch (requestCode) {
            case 0:
                refreshLayout.autoRefresh();
                break;

        }
    }
}
